package com.github.binarywang.demo.wx.pay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.demo.wx.pay.utils.ApacheHttp;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.marketing.FavorCouponsQueryRequest;
import com.github.binarywang.wxpay.bean.marketing.FavorCouponsQueryResult;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.MarketingFavorService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Binary Wang
 */
@Api("微信支付")
@RestController
@RequestMapping("/wxpay/{appid}")

public class WxPayController {

  @Resource
  private Map<String, WxPayServiceImpl> wxPayServiceMap;


  @Value("${myapi.notifyurl}")
  private String notifyurl;
  /**
   * 调用统一下单接口，并组装生成支付所需参数对象.
   *
   * @param request 统一下单请求参数
   * @param <T>     请使用{@link com.github.binarywang.wxpay.bean.order}包下的类
   * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
   */
  @ApiOperation(value = "统一下单，并组装所需支付参数")
  @PostMapping("/createOrder")
  public <T> T createOrder(@PathVariable String appid, @RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
    WxPayService wxService = (WxPayService) wxPayServiceMap.get(appid);
    if (wxService == null) {
        throw new WxPayException("APPID不存在");
    }
    return wxService.createOrder(request);
  }


  @ApiOperation(value = "支付回调通知处理")
  @PostMapping("/notify/order")
  public String parseOrderNotifyResult(@PathVariable String appid, @RequestBody String xmlData) throws WxPayException {
    WxPayService wxService = (WxPayService) wxPayServiceMap.get(appid);
    if (wxService == null) {
      throw new WxPayException("APPID不存在");
    }
    final WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);
    // TODO 根据自己业务场景需要构造返回对象
    JSONObject jsapiSign = new JSONObject();

    jsapiSign.put("openid", notifyResult.getOpenid());

    jsapiSign.put("totalFee", notifyResult.getTotalFee());

    jsapiSign.put("transactionId", notifyResult.getTransactionId());
    jsapiSign.put("outTradeNo", notifyResult.getOutTradeNo());

    jsapiSign.put("errCodeDes", notifyResult.getErrCodeDes());

    jsapiSign.put("resultCode", notifyResult.getResultCode());

    jsapiSign.put("appid", notifyResult.getAppid());

    jsapiSign.put("mchId", notifyResult.getMchId());

    jsapiSign.put("sign", notifyResult.getSign());


    String ret = ApacheHttp.post(notifyurl, JSON.toJSONString(jsapiSign));
    return WxPayNotifyResponse.success("成功");
  }

  @GetMapping("/test")
  public String test() {
    return "ok"+notifyurl;
  }

  @GetMapping("/stest")
  public String stest() {
    return "sok";
  }


}

